package com.zookeeper.exception;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by eason on 2017/7/27.
 */
@Component
public class HandleException extends ExceptionHandlerExceptionResolver {

    public ModelAndView doResolveHandlerMethodException(HttpServletRequest request,
                                                           HttpServletResponse response, HandlerMethod handlerMethod, Exception exception) {

       BizException ex =(BizException)exception;
        ModelAndView mv = new ModelAndView();
        Map map =new HashMap();
        map.put("code", ex.getErrorCode());
        map.put("msg", ex.getMsg());
        FastJsonJsonView view = new FastJsonJsonView();
        view.setAttributesMap(map);
        mv.setView(view);

        return mv;
    }

    }
